var addBlog = {
    WangEditor:null,
    data:null,
    init : function () {
        addBlog.creatEditor();
    },
    creatEditor:function () {
        var E = window.wangEditor
       addBlog.WangEditor = new E('#editor');
        addBlog.WangEditor.customConfig.uploadImgServer ='/article/upload';
        addBlog.WangEditor.uploadFileName='file';

        var text = document.getElementById("text");
        addBlog.WangEditor.customConfig.onchange=function(html){
            text.value=html;

        }
        addBlog.WangEditor.create();
        var content = document.getElementById('tet');
        if (content == null){
            addBlog.WangEditor.txt.html("<p>请输入内容...</p>")
        }else{
            addBlog.WangEditor.txt.html(content.innerText)
        }

        text.value=addBlog.WangEditor.txt.html();
    },
    saveBlog:function(statu){
        //获取选中的checkBox
        var checkbox = document.getElementsByName('label');
        var labels = "";
        for (i in checkbox){
            if (checkbox[i].checked && checkbox[i].value!=undefined){
                labels+=checkbox[i].value+",";
            }
        }
        //获取单选框
        var rabio = document.getElementsByName('type');
        var id = document.getElementById('id').value;
        var type = "";
        for (j in rabio){
            if (rabio[j].checked && rabio[j].value!= undefined){
                type=rabio[j].value;
            }
        }
        if(labels!=""){
            labels=labels.substr(0,labels.length-1);
        }
        var title = $("#title").val();
        var simpleContent = addBlog.WangEditor.txt.text().substr(0,100);
        var content = addBlog.WangEditor.txt.html();
        addBlog.data={
            id:id,
            title:title,
            simpleContent:simpleContent,
            content:content,
            typeId:type,
            statu:statu,
            labels:labels
        }
        addBlog.save();
    },
    save:function () {
        if(addBlog.validate(addBlog.data)){
            $.ajax({
                type:'POST',
                url:"/article/add",
                data:addBlog.data,
                dataType:'json',
                async:false,
                success:function (result) {
                    if(result.code == 200){
                        layer.msg(result.msg,{time:2000,icon:1});
                        location.href="/article/list"
                    }else if(result.code ==101){
                        layer.msg(result.msg,{time:2000,icon:2});
                    }else if (result.code == 500){
                        layer.msg(result.msg,{time:200000,icon:5});
                    }
                }
            })
        }
    },
    validate:function (data) {
        var flag = true;
        if(data.title ==null || data.title == ""){
            layer.tips('标题不能为空', '#title');
            flag= false;
        }else if(data.labels == null || data.labels == ""){
            layer.tips('请选择标签', '#labels');
            flag= false;
        } else if (data.typeId == null || data.typeId == ""){
            layer.tips('请选择类型', '#type');
            flag= false;
        }
        return flag;
    }
}